<?php
class Administrator_news_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	/*Type Management*/
	function type_management_add()
	{
		$news_type_name = $this->input->post('news_type_name');
		$datetime = date('Y-m-d H:i:s');
		
		//Insert news_type
		$data = array(
			'news_type_name' => $news_type_name,
			'update_by' => $this->session->userdata('username'),			
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('news_type',$data);
	}
	
	function type_management_edit($news_type_id)
	{
		$news_type_name = $this->input->post('news_type_name');
		$datetime = date('Y-m-d H:i:s');
		
		//Update news_type
		$data = array(
			'news_type_name' => $news_type_name,
			'update_by' => $this->session->userdata('username'),			
			'update_date' => $datetime
		);
		$this->db->update('news_type',$data,array('news_type_id' => $news_type_id));				
	}	
	
	function type_management_delete()
	{
		$id = $this->input->post('id');
		foreach($id as $value):
			$tables = array('news_type');
			$this->db->where('news_type_id', $value);
			$this->db->delete($tables);			
		endforeach;			
	}		
	
	/*Content Management*/
	function content_management_add()
	{
		$news_type_id = $this->input->post('news_type_id');
		$news_publish_date = $this->input->post('news_publish_date');
		$is_publish = $this->input->post('is_publish');
		$is_main_hilight = $this->input->post('is_main_hilight');
		$is_type_hilight = $this->input->post('is_type_hilight');
		$is_review_hilight = $this->input->post('is_review_hilight');
		$author_by = $this->input->post('author_by');
		$news_subject = $this->input->post('news_subject');
		$news_brief = $this->input->post('news_brief');
		$news_detail = $this->input->post('news_detail');
		$news_tag = $this->input->post('news_tag');
		$news_event_days = $this->input->post('news_event_days');
		$artist_id = $this->input->post('artist_id');
		$news_relate_module = $this->input->post('news_relate_module');
		$news_relate_id = $this->input->post('news_relate_id');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert news
		$data = array(
			'news_type_id' => $news_type_id,
			'news_publish_date' => convertDatetime($news_publish_date),
			'is_publish' => ''.$is_publish.'',
			'is_main_hilight' => ''.$is_main_hilight.'',
			'is_type_hilight' => ''.$is_type_hilight.'',
			'is_review_hilight' => ''.$is_review_hilight.'',
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('news',$data);
		$news_id = $this->db->insert_id();
		
		//Insert news_info
		$data = array(
			'news_id' => $news_id,
			'news_subject' => $news_subject,
			'news_brief' => $news_brief,			
			'news_detail' => $news_detail,
			'news_tag' => $news_tag,
			'news_relate' => '',
			'author_by' => ''.$author_by.''			
		);
		$dataJson = array();
		foreach($news_relate_module as $key => $value):
			if(!empty($value) && !empty($news_relate_id[$key])):
				$dataJson[] = array(
					'module' => $value,
					'value' => $news_relate_id[$key]
				);
			endif;
		endforeach;
		if(!empty($dataJson)) $data['news_relate'] = json_encode($dataJson);
		$this->db->insert('news_info',$data);
		
		//Insert news_artist
		$dataArtist = array();
		if(!empty($artist_id)) foreach($artist_id as $value):
			$dataArtist[] = array(
				'news_id' => $news_id,
				'artist_id' => $value
			);
		endforeach;
		if(!empty($dataArtist))	$this->db->insert_batch('news_artist',$dataArtist);	
			
		//Insert news_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $news_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'news/images/',
				'dimension' => array(												
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'news_id' => $news_id,
				'news_file_name' => $data->file_name,
				'news_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('news_image',$dataImage);
		
		//Update Solr
		$this->load->library('solr');	
		
		$document = new Apache_Solr_Document();
		$document->id = 'news_'.$news_id;
		$document->news_type_id_i = $news_type_id;
		$document->news_name_s = $news_subject;	
		$document->news_short_desc_s = $news_brief;
		$document->news_tag_s = $news_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($news_publish_date)) $document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime(convertDatetime($news_publish_date)));

		$artist_id_i_arr = array();
		if(!empty($artist_id)) foreach($artist_id as $value):
			$artist_id_i_arr[] = $value;	
		endforeach;
		$document->artist_id_i_arr = $artist_id_i_arr;
		if(!empty($dataImage)) $document->news_image_name_s = $dataImage[0]['news_file_name'].'.'.$dataImage[0]['news_file_ext'];
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Delete Memcache
		$this->memcache->delete('dataNewsHilight');
		$this->memcache->delete('dataNewsUpdate');
		$this->memcache->delete('dataNewsArticle');
		$this->memcache->delete('dataNewsAlbumReview');
		$this->memcache->delete('dataNewsThai');
		$this->memcache->delete('dataNewsInter');
		$this->memcache->delete('dataNewsAsian');
		$this->memcache->delete('dataNewsMovie');
		$this->memcache->delete('dataModuleMovieNews');
		$this->memcache->delete('dataModuleNewsHot');
		$this->memcache->delete('dataAlbumReview');		
		$this->memcache->delete('dataHomeAlbumReview');	
		$this->memcache->delete('dataHomeNews');		
		$this->memcache->delete('dataHomeNewsMovie');
		
		//Redirect to Edit Page
		redirect('news/content_management/edit/'.$news_id);	
	}
	
	function content_management_edit($news_id)
	{
		$news_type_id = $this->input->post('news_type_id');
		$news_publish_date = $this->input->post('news_publish_date');
		$is_publish = $this->input->post('is_publish');
		$is_main_hilight = $this->input->post('is_main_hilight');
		$is_type_hilight = $this->input->post('is_type_hilight');
		$is_review_hilight = $this->input->post('is_review_hilight');
		$author_by = $this->input->post('author_by');
		$news_subject = $this->input->post('news_subject');
		$news_brief = $this->input->post('news_brief');
		$news_detail = $this->input->post('news_detail');
		$news_tag = $this->input->post('news_tag');
		$artist_id = $this->input->post('artist_id');
		$news_relate_module = $this->input->post('news_relate_module');
		$news_relate_id = $this->input->post('news_relate_id');		
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');

		//Insert to news
		$data = array(
			'news_type_id' => $news_type_id,
			'news_publish_date' => convertDatetime($news_publish_date),
			'is_publish' => ''.$is_publish.'',
			'is_main_hilight' => ''.$is_main_hilight.'',
			'is_type_hilight' => ''.$is_type_hilight.'',
			'is_review_hilight' => ''.$is_review_hilight.'',
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('news',$data,array('news_id' => $news_id));
		
		//Update news_info
		$data = array(
			'news_id' => $news_id,
			'news_subject' => $news_subject,
			'news_brief' => $news_brief,			
			'news_detail' => $news_detail,
			'news_tag' => $news_tag,
			'news_relate' => '',
			'author_by' => ''.$author_by.''
		);
		$dataJson = array();
		foreach($news_relate_module as $key => $value):
			if(!empty($value) && !empty($news_relate_id[$key])):
				$dataJson[] = array(
					'module' => $value,
					'value' => $news_relate_id[$key]
				);
			endif;
		endforeach;
		if(!empty($dataJson)) $data['news_relate'] = json_encode($dataJson);		
		$this->db->update('news_info',$data,array('news_id' => $news_id));
		
		//Delete news_artist
		$this->db->delete('news_artist',array('news_id' => $news_id));
		//Update news_artist
		$dataArtist = array();
		if(!empty($artist_id)) foreach($artist_id as $value):
			$dataArtist[] = array(
				'news_id' => $news_id,
				'artist_id' => $value
			);
		endforeach;
		if(!empty($dataArtist))	$this->db->insert_batch('news_artist',$dataArtist);	
		
		//Insert news_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $news_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'news/images/',
				'dimension' => array(											
					array(
						'width' => 120,
						'height' => 120
					)				
				)
			));		
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'news_id' => $news_id,
				'news_file_name' => $data->file_name,
				'news_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('news_image',$dataImage);
		
		//Update Solr
		$this->load->library('solr');	
		
		$document = new Apache_Solr_Document();
		$document->id = 'news_'.$news_id;
		$document->news_type_id_i = $news_type_id;
		$document->news_name_s = $news_subject;	
		$document->news_short_desc_s = $news_brief;
		$document->news_tag_s = $news_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($news_publish_date)) $document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime(convertDatetime($news_publish_date)));

		$artist_id_i_arr = array();
		if(!empty($artist_id)) foreach($artist_id as $value):
			$artist_id_i_arr[] = $value;	
		endforeach;
		$document->artist_id_i_arr = $artist_id_i_arr;
		//Image	
		$dataNewsImage = $this->db->order_by('news_image_order ASC,news_image_id ASC')->
		get_where('news_image',array('news_id' => $news_id),1)->
		row();
		
		$dataImage = $this->db->select('news_file_name,news_file_ext')->order_by('news_image_order ASC,news_image_id ASC')->get_where('news_image',array('news_id' => $news_id),1)->row();
		if(!empty($dataImage->news_file_name)) $document->news_image_name_s = $dataImage->news_file_name.'.'.$dataImage->news_file_ext;

		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Delete Memcache
		$this->memcache->delete('dataNewsHilight');
		$this->memcache->delete('dataNewsUpdate');
		$this->memcache->delete('dataNewsArticle');		
		$this->memcache->delete('dataNewsAlbumReview');
		$this->memcache->delete('dataNewsThai');
		$this->memcache->delete('dataNewsInter');
		$this->memcache->delete('dataNewsAsian');
		$this->memcache->delete('dataNewsMovie');
		$this->memcache->delete('dataModuleMovieNews');	
		$this->memcache->delete('dataModuleNewsHot');
		$this->memcache->delete('dataAlbumReview');	
		$this->memcache->delete('dataHomeAlbumReview');
		$this->memcache->delete('dataHomeNews');
		$this->memcache->delete('dataHomeNewsMovie');								
	}	
	
	function ajax_update_image()
	{
		$news_image_id = $this->input->post('news_image_id');
		if(!empty($news_image_id)) foreach($news_image_id as $key => $value):
			$this->db->update('news_image',array('news_image_order' => $key),array('news_image_id' => $value));
		endforeach;
		exit;
	}
	
	function ajax_delete_image($news_image_id)
	{
		$dataImage = $this->db->get_where('news_image',array('news_image_id' => $news_image_id),1)->row();
		$this->db->delete('news_image',array('news_image_id' => $news_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'news/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'news/images/'.$folder.'/'.$dataImage->news_file_name.'.'.$dataImage->news_file_ext);
		endforeach;
		exit;
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'news/images/', 1);
		$ids = array();
		foreach($id as $value):
			$ids[] = "news_".$value;
			$dataImage = $this->db->get_where('news_image',array('news_id' => $value))->result();
			$tables = array('news','news_info','news_artist','news_image','news_click_stat');
			$this->db->where('news_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				foreach($dataImage as $items):
					@unlink($this->upload_folder.'news/images/'.$folder.'/'.$items->news_file_name.'.'.$items->news_file_ext);
				endforeach;
			endforeach;							
		endforeach;
		
		//Update Solr
		$this->load->library('solr');	
		if($this->solr->ping() && !empty($ids)):
			$this->solr->deleteByMultipleIds($ids);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		//Delete Memcache
		$this->memcache->delete('dataNewsHilight');
		$this->memcache->delete('dataNewsUpdate');
		$this->memcache->delete('dataNewsArticle');		
		$this->memcache->delete('dataNewsAlbumReview');
		$this->memcache->delete('dataNewsThai');
		$this->memcache->delete('dataNewsInter');
		$this->memcache->delete('dataNewsAsian');
		$this->memcache->delete('dataNewsMovie');
		$this->memcache->delete('dataModuleMovieNews');					
		$this->memcache->delete('dataModuleNewsHot');
		$this->memcache->delete('dataAlbumReview');	
		$this->memcache->delete('dataHomeAlbumReview');						
		$this->memcache->delete('dataHomeNews');
		$this->memcache->delete('dataHomeNewsMovie');						
	}
	/*End Content Management*/
}

?>